package end.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import end.bean.shiro.Resource;

/**
 * 
 * ResourceTree 上午10:00:07 Copyright Zhaocj Inc. All right reserved. Email :
 * alldios@outlook.com
 * 
 */
public class ResourceTree implements Serializable {
	private static final long serialVersionUID = 6145961932017984738L;
	public static Map<String, Object> mapArray = new LinkedHashMap<String, Object>();
	public List<Resource> menuCommon;
	public List<Object> list = new ArrayList<Object>();

	@SuppressWarnings("unchecked")
	public List<Object> menuList(List<Resource> resource) {
		this.menuCommon = resource;
		for (Resource x : resource) {
			Map<String, Object> mapArr = new LinkedHashMap<String, Object>();
			if (x.getParentId() == 0) {
				mapArr.put("id", x.getId());
				mapArr.put("text", x.getName());
				mapArr.put("type", x.getType());
				mapArr.put("code", x.getCode());
				mapArr.put("url", x.getUrl());
				// 如果他有子节点的话
				if (menuChild(x.getId()).size() > 0) {
					boolean isChecked = true;
					for (Object obj : menuChild(x.getId())) {
						Map<String, Object> childArr = (LinkedHashMap<String, Object>) obj;
						if ((boolean) childArr.get("checked") == false) {
							isChecked = false;
							break;
						}
					}
					mapArr.put("checked", isChecked);
				} else {
					mapArr.put("checked", x.isChecked());
				}
				if (!menuChild(x.getId()).isEmpty()) {
					mapArr.put("iconCls", "icon-parent-1");
					mapArr.put("state", "open");
				} else {
					mapArr.put("iconCls", "icon-child-1");
				}
				if (!menuChild(x.getId()).isEmpty()) {
					mapArr.put("children", menuChild(x.getId()));
				} else {
					mapArr.put("children", null);
				}
				list.add(mapArr);
			}
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<?> menuChild(int id) {
		List<Object> lists = new ArrayList<Object>();
		for (Resource a : menuCommon) {
			Map<String, Object> childArray = new LinkedHashMap<String, Object>();
			if (a.getParentId() == id) {
				childArray.put("id", a.getId());
				childArray.put("text", a.getName());
				childArray.put("type", a.getType());
				childArray.put("code", a.getCode());
				childArray.put("url", a.getUrl());
				// 如果他有子节点的话
				if (menuChild(a.getId()).size() > 0) {
					boolean isChecked = true;
					for (Object obj : menuChild(a.getId())) {
						Map<String, Object> childArr = (LinkedHashMap<String, Object>) obj;
						if ((boolean) childArr.get("checked") == false) {
							isChecked = false;
							break;
						}
					}
					childArray.put("checked", isChecked);
				} else {
					childArray.put("checked", a.isChecked());
				}
				if (!menuChild(a.getId()).isEmpty()) {
					childArray.put("iconCls", "icon-parent-1");
					childArray.put("state", "open");
				} else {
					childArray.put("iconCls", "icon-child-1");
				}
				if (!menuChild(a.getId()).isEmpty()) {
					childArray.put("children", menuChild(a.getId()));
				} else {
					childArray.put("children", null);
				}
				lists.add(childArray);
			}
		}
		return lists;
	}
}
